import time

from zn.common.component.component import TComponent , componentManager
from fastapi import APIRouter
from zn.common.router.root import app
from zn.common.component.db import TDB

class TTerminal(TComponent):
    def __init__(self , _mananger):
        super().__init__(_mananger)
        self.channels={}
        self.mqtts ={}

    @property
    def terCode(self):
        res = ""
        try:
            res = self.settings['terCode']
        except Exception as er:
            self.manager.error(er)
        return res
    @property
    def db(self)->TDB:
        dbCode= self.settings['dbCode']
        return  componentManager.getComponent(dbCode)
    def start(self):
        super().start()
        try:
            pass


        except Exception as er:
            self.manager.error(er)
    def init(self):
        super().init()
        try:
            self.loadChannels()
        except Exception as er:
            self.manager.error(er)
    def stop(self):
        super().stop()
        try:
            pass
        except Exception as er:
            self.manager.error(er)
    def getStatus(self):
        res = super().getStatus()
        try:
            res['status']={
                "time": time.time()
            }
        except Exception as er:
            self.manager.error(er)
        return res



    def loadChannels(self):
        try:
            pass
        except Exception as er:
            self.manager.error(er)




router = APIRouter()
@router.get("/info/{terCode}")
async def iotInfo(terCode):
    return TTerminal.getTerStatus(terCode)
app.include_router(router, prefix="/iot" ,tags=['iot'])


componentManager.registComponentType('TTerminal', TTerminal)